<?php
class ControllerSaleRechargeablecard extends AdminController {

  	public function index() {  
		M('sale/rechargeablecard');
		$this->getList();
  	}

  	public function insert() {

	  	M('sale/rechargeablecard');
    	if ($this->request->isPost()&&($this->validateForm())) {
			$this->model_sale_rechargeablecard->addRechargeablecard($this->request->post);
			$this->session->set_flashdata('success', $this->_['text_success_insert']);
			$this->redirect(UA('sale/rechargeablecard'));
    	}
    	$this->getForm();
  	}

  	public function update() {

		M('sale/rechargeablecard');

    	if ($this->request->isPost() && $this->validateAmount()) {


			$this->model_sale_rechargeablecard->editRechargeablecard($this->request->get['cardNumber'], $this->request->post);

            $this->session->set_flashdata('success', $this->_['text_success_update']);
			$this->redirect(UA('sale/rechargeablecard'));
		}

    	$this->getForm();
  	}

    public function delete() {

		M('sale/rechargeablecard');

    	if (isset($this->request->post['selected']) ) {
            $pass=true;
			foreach ($this->request->post['selected'] as $cardNumber) {
               if($this->validateDelete($cardNumber)) {
				$this->model_sale_rechargeablecard->deleteRechargeablecard($cardNumber);
               }else{
                   $pass=false;
               }
			}
            if($pass){
                $this->session->set_flashdata('success', $this->_['text_success_delete']);
            }else{
                $this->session->set_flashdata('success', $this->_['error_cardDelete']);
            }
			$this->redirect(UA('sale/rechargeablecard'));
    	}

    	$this->getList();
  	}

    public function autocreate(){


    	M('sale/rechargeablecard');

    	if ($this->request->isPost() && ($this->validateForm2()) ){
            $cardNumbers=$this->create_cardNumbers($this->request->post['cardCount']);

            foreach($cardNumbers as $key=>$value) {
            $this->data['cardNumber'] = $key;
		        $this->data['action'] = UA('sale/rechargeablecard/update');
		        $this->data['cardCode'] = $this->create_password();
		        $this->data['cardAmount'] = $this->request->post['cardAmount'];
	         	$this->data['cardStatus'] = P('cardStatus',0);
		        $this->data['cardCustomer'] =P('cardCustomer','');


            	$this->model_sale_rechargeablecard->addRechargeablecard($this->data);
            }
			$this->session->set_flashdata('success', $this->_['text_success_insert']);
			$this->redirect(UA('sale/rechargeablecard'));
		}
    	$this->getForm2();
    }


  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$this->data['rechargeablecards'] = array();

		$rechargeablecard_total = $this->model_sale_rechargeablecard->getTotalRechargeablecards();//获取总条数

		$results = $this->model_sale_rechargeablecard->getRechargeablecards($qf);

    	foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->_['text_edit'],
				'href' => UA('sale/rechargeablecard/update', 'cardNumber=' . $result['cardNumber'])
			);

			$this->data['rechargeablecards'][] = array(
				'cardNumber' => $result['cardNumber'],
				'cardCode'       => $result['cardCode'],
				'cardAmount'     => $this->currency->format($result['cardAmount'], C('config_currency')),
				'cardStatus'     => $result['cardStatus'],
				'cardCustomer'=>$result['cardCustomer'],
				'cardDate' => v5_date_format($result['cardDate']),
				'action'     => $action
			);
		}

		$this->data['pagination'] = $this->pagination($rechargeablecard_total)->render();

		$this->display('sale/rechargeablecard_list.tpl');
  	}

 
  		protected function getForm() {

		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['cardNumber'])) {
			$this->data['cardNumber'] = $this->request->get['cardNumber'];
			$this->data['action'] = UA('sale/rechargeablecard/update', 'cardNumber=' . $this->request->get['cardNumber']);
		} else {
            $this->data['cardNumber'] =P('cardNumber','1000');
			$this->data['action'] = UA('sale/rechargeablecard/insert');
		}

		if (isset($this->request->get['cardNumber']) && !$this->request->isPost()) {
      		$card_info = $this->model_sale_rechargeablecard->getRechargeablecard($this->request->get['cardNumber']);
    	}

    	if (!empty($card_info)) {
			$this->data['cardNumber'] = $card_info['cardNumber'];
			$this->data['cardCode'] = $card_info['cardCode'];
			$this->data['cardAmount'] = $card_info['cardAmount'];
			$this->data['cardStatus'] = $card_info['cardStatus'];
			$this->data['cardCustomer'] = $card_info['cardCustomer'];
		} else {
			$this->data['cardCode'] =P('cardCode');
			$this->data['cardAmount'] = P('cardAmount');
			$this->data['cardStatus'] = P('cardStatus');
			$this->data['cardCustomer'] =P('cardCustomer','');
    	}

		$this->display('sale/rechargeablecard_form.tpl');
  	}


  	protected function getForm2(){
  		$this->document->setTitle($this->_['heading_title']);
  		$this->data['cardCount']=P('cardCount');
  		$this->data['cardAmount']=P('cardAmount');
        $this->data['action'] = UA('sale/rechargeablecard/autocreate');
		$this->display('sale/rechargeablecard_form_auto.tpl');
  	}



    protected function validateForm() {
		$pass = true;

         if (strlen($this->request->post['cardNumber']) !=12||(!is_numeric($this->request->post['cardNumber']))) {
      		$this->setMessage('error_cardNumber', $this->_['error_cardNumber']);
			$pass = false;
    	}

        if(strpos($this->request->post['cardNumber'], '1000') !== 0){
            $this->setMessage('error_cardNumber', $this->_['error_cardNumber1']);
            $pass = false;
        }

        if(!$this->validateCardNumber($this->request->post['cardNumber'])){
            $this->setMessage('error_cardNumber', $this->_['error_cardNumber2']);
            $pass = false;
        }

    	if (strlen($this->request->post['cardCode']) !=6){
      		$this->setMessage('error_cardCode', $this->_['error_cardCode']);
			$pass = false;
    	}

    	if ($this->request->post['cardAmount'] < 1||$this->request->post['cardAmount'] >2000) {
      		$this->setMessage('error_cardAmount', $this->_['error_cardAmount']);
			$pass = false;
    	}

    	return $pass;
  	}


  	 protected function validateForm2() {

		$pass = true;
		if (($this->request->post['cardCount'] <1) || ($this->request->post['cardCount']>99999999)) {
      		$this->setMessage('error_cardCount', $this->_['error_cardCount']);
			$pass = false;
    	}

    	if ($this->request->post['cardAmount'] < 1) {
      		$this->setMessage('error_cardAmount', $this->_['error_cardAmount']);
			$pass = false;
    	}

    	return $pass;
  	}


    protected function validateCardNumber($cardNumber){

        $card_info=$this->model_sale_rechargeablecard->getRechargeablecard($cardNumber);
        if(!empty($card_info)){
        	$this->setMessage('error_cardExists', $this->_['error_cardExists']);
        	return false;
        }
        return true;
    }

    protected function validateAmount() { //修改的时候用

        $pass = true;
        if ($this->request->post['cardAmount'] < 1||($this->request->post['cardAmount']>2000)) {
            $this->setMessage('error_cardAmount', $this->_['error_cardAmount']);
            $pass = false;
        }
        return $pass;
    }

    protected function validateDelete($cardNumber) {

		M('sale/rechargeablecard');

			$card_info = $this->model_sale_rechargeablecard->getRechargeablecard($cardNumber);
			if ($card_info['cardStatus']==1) {
				$this->setMessage('error_CardDelete', $this->_['error_CardDelete']);
				return false;
			}

		return true;
  	}


   protected function create_cardNumbers($count=0){

       M('sale/rechargeablecard');
       set_time_limit(0);
       $card_info= $this->model_sale_rechargeablecard->getCard_prefix();

       $cardNumbers=array();
       $i=0;
       do{
        $cardNumber = $card_info['card_prefix'].substr(base_convert( md5($i*$card_info['card_prefix']), 16, 10 ), 1,8);
        $cardNumbers[$cardNumber]=1;
        $i++;
       }while($i!=$count);

       $this->model_sale_rechargeablecard->setCard_prefix( $card_info['card_prefix']);
       return $cardNumbers;
    }


    protected function create_password() {
       $chars = '0123456789abcdefghijklmnopqrstuvwxyz';
       $cardCode='';
          for ( $i = 0; $i <6; $i++ )  {  
            $cardCode .= $chars[ mt_rand(0, strlen($chars) - 1) ];
          }
      
       return $cardCode;
  }
   
}
?>